#ifndef ONLINECONNECTOR_H
#define ONLINECONNECTOR_H

#include <QObject>
#include <QWidget>
#include <QtNetwork>
#include <QtSql>
#include <QStatusBar>
#include <QLabel>
#include <QProgressBar>
#include <QMessageBox>

class OnlineConnector : public QObject
{
    Q_OBJECT

public:
    explicit OnlineConnector(QStatusBar *statusbar, QObject *parent = 0);

    void login();
    bool isLogedIn() const;

    void setUsername(const QString &value);
    void setPassword(const QString &value);
    bool isValid() const;

    QNetworkAccessManager *getManager() const;
    QList<QNetworkCookie> getCookies() const;

Q_SIGNALS:
    void login_success();
    void login_failed(QString error = QString());
    void login_valid();

public Q_SLOTS:
    void reconnect();

private Q_SLOTS:
    void replyFinished();

private:
    QSqlDatabase db;
    QString username;
    QString password;

    QNetworkAccessManager *mManager;
    QUrl mUrl;
    QNetworkReply *mReply;
    QList<QNetworkCookie> session_cookies;

    QStatusBar *statusbar;
    QLabel *login_progress_label;
    QProgressBar *login_progress_bar;

    bool logedIn;
};

#endif // ONLINECONNECTOR_H
